﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.BusinessLogic;
namespace HRM
{
    public partial class frmMauepChucDanh_Edit : FormBase
    {
        public frmMauepChucDanh_Edit()
        {
            InitializeComponent();
        }        private DataTable m_dtChucDanh;
        public DialogResult Show_Form(DataTable dtChucDanh)
        {
            m_dtChucDanh = dtChucDanh;
            if (m_dtChucDanh.Rows.Count > 0)
            {
                m_dtChucDanh.DefaultView.RowFilter = "Chon =1";
                m_dtChucDanh.DefaultView.ToTable();
            }
            return this.ShowDialog();
        }
        DataGridHandle m_GridHandle;
        protected override void Init_Control()
        {
            FormGlobals.TextBox_SetHandle(DataInputType.DateData, txtFrom_Date, txtTo_Date);
            FormGlobals.TextBox_SetFormat(Globals.CS_DISPLAY_MONTHYEAR_FORMAT, txtFrom_Date, txtTo_Date);
            FormGlobals.TextBox_GetHandle(txtFrom_Date).DateTime_SetVisibleIcon(false);
            FormGlobals.TextBox_GetHandle(txtTo_Date).DateTime_SetVisibleIcon(false);
            FormGlobals.Control_SetRequired(txtChucDanh);
            //Autocomplete danh sách chức danh
            int counter = 0;
            DataTable dtGrade = Grade_Controller.Instance().Grade_GetList(out counter, Program.strBranchCode, 0, 9999).Tables[0];
            new AutoCompleteTextBox_Handle(txtChucDanh, dtGrade, "Grade_Name");
            //
            dgvBrowse.Columns.AddRange(
               FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Họ và tên", "HOTEN", 120),
               FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Số sổ BHXH", "SOBHXH", 80),
               FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Ngày sinh", "NGAYSINH", 70, Globals.CS_DISPLAY_DATE_FORMAT),
               FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Từ tháng", "TUTHANG", 60, Globals.CS_EDIT_MONTHYEAR_FORMAT),
               FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Đến tháng", "DENTHANG", 60, Globals.CS_EDIT_MONTHYEAR_FORMAT),
               FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mã phòng ban", "MAPB", 120));
            m_GridHandle = FormGlobals.Grid_Init(dgvBrowse, "");
            Bind_data();
        }
        private void Bind_data()
        {
            FormGlobals.App_ProgressShow(delegate()
            {
                if (m_dtChucDanh != null)
                    dgvBrowse.DataSource = m_dtChucDanh;
            }, "Đang tải dữ liệu", "Hoàn thành");
        }
        private void btnClose_click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!FormGlobals.Panel_CheckError(grbInfo))
                    return;
                FormGlobals.App_ProgressSave(delegate()
                {
                    foreach (DataRow drow in m_dtChucDanh.Rows)
                    {
                        if (Int32.Parse(drow["Chon"].ToString()) > 0)
                        {
                            Employee_Controller.Instance().Employee_UpdateChucDanh(
                                0
                                ,""
                                , Globals.DB_GetValue<String>(drow["SOBHXH"], "")
                                , FormGlobals.Control_GetValue<DateTime>(txtFrom_Date, DateTime.Parse(drow["TUTHANG"].ToString()))
                                , FormGlobals.Control_GetValue<DateTime>(txtTo_Date, DateTime.Parse(drow["DENTHANG"].ToString()))
                                , txtChucDanh.Text);
                        }
                    }
                });
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex) { FormGlobals.Message_Error(ex); this.DialogResult = DialogResult.Cancel; }
        }
    }
}
